package com.hdu.other.oneDimensionPreInfo;

import java.util.HashMap;
import java.util.Map;

public class lc1124 {


    class Solution {
        public int longestWPI(int[] hours) {
            Map<Integer, Integer> preSum2firstIndex = new HashMap<>();
            preSum2firstIndex.put(0, -1);
            int sum = 0;
            int ans = 0;
            for (int i = 0; i < hours.length; i++) {
                sum += hours[i] > 8 ? 1 : -1;
                if (sum > 0) {
                    ans = i + 1;
                } else {
                    if (preSum2firstIndex.containsKey(sum - 1)) {
                        ans = Math.max(ans, i - preSum2firstIndex.get(sum - 1));
                    }
                    if (!preSum2firstIndex.containsKey(sum)) {
                        preSum2firstIndex.put(sum, i);
                    }
                }
            }
            return ans;
        }
    }

}
